# gui/widgets/status_widget.py
"""
状态显示组件 - 显示系统运行状态和进度
"""

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class StatusIndicator(QWidget):
    """状态指示器组件"""

    def __init__(self, name: str, parent=None):
        super().__init__(parent)
        self.name = name
        self.status = "idle"  # idle, running, completed, error
        self.init_ui()

    def init_ui(self):
        """初始化UI"""
        layout = QHBoxLayout(self)
        layout.setContentsMargins(5, 2, 5, 2)

        # 状态指示圆点
        self.indicator = QLabel("●")
        self.indicator.setFixedSize(12, 12)

        # 名称标签
        self.name_label = QLabel(self.name)
        self.name_label.setObjectName("StatusIndicatorLabel")

        layout.addWidget(self.indicator)
        layout.addWidget(self.name_label)
        layout.addStretch()

        self.update_indicator()

    def set_status(self, status: str):
        """设置状态"""
        self.status = status
        self.update_indicator()

    def update_indicator(self):
        """更新指示器显示"""
        colors = {
            "idle": "#6c757d",      # 灰色
            "running": "#28a745",   # 绿色
            "completed": "#007bff", # 蓝色
            "error": "#dc3545"      # 红色
        }

        color = colors.get(self.status, "#6c757d")
        self.indicator.setStyleSheet(f"color: {color}; font-size: 10px;")

class ProgressBarWidget(QWidget):
    """进度条组件"""

    def __init__(self, parent=None):
        super().__init__(parent)
        self.init_ui()

    def init_ui(self):
        """初始化UI"""
        layout = QVBoxLayout(self)
        layout.setContentsMargins(0, 5, 0, 5)

        # 进度标签
        self.progress_label = QLabel("准备就绪")
        self.progress_label.setStyleSheet("font-size: 10px; color: #495057; margin-bottom: 3px;")

        # 进度条
        self.progress_bar = QProgressBar()
        self.progress_bar.setRange(0, 100)
        self.progress_bar.setValue(0)
        self.progress_bar.setStyleSheet("""
            QProgressBar {
                border: 1px solid #dee2e6;
                border-radius: 3px;
                text-align: center;
                font-size: 9px;
                color: #495057;
                background-color: #f8f9fa;
                height: 16px;
            }
            QProgressBar::chunk {
                background-color: #007bff;
                border-radius: 2px;
            }
        """)

        layout.addWidget(self.progress_label)
        layout.addWidget(self.progress_bar)

    def update_progress(self, operation: str, progress: int):
        """更新进度"""
        self.progress_label.setText(f"{operation}...")
        self.progress_bar.setValue(progress)

    def set_idle(self):
        """设置空闲状态"""
        self.progress_label.setText("准备就绪")
        self.progress_bar.setValue(0)


class CompactStatusBar(QWidget):
    """紧凑状态栏"""

    def __init__(self, parent=None):
        super().__init__(parent)
        self.init_ui()

    def init_ui(self):
        """初始化UI"""
        layout = QHBoxLayout(self)
        layout.setContentsMargins(10, 2, 10, 2)

        # 主状态标签
        self.main_status = QLabel("系统就绪")
        self.main_status.setStyleSheet("font-size: 11px; color: #495057;")

        # 雷达工作模式显示标签
        self.radar_mode_label = QLabel("雷达模式: 搜索")
        self.radar_mode_label.setStyleSheet("font-size: 11px; color: #28a745; font-weight: bold;")
        self.radar_mode_label.setVisible(False)  # 默认隐藏，仿真时显示

        # 仿真时间显示标签
        self.sim_time_label = QLabel("仿真时间: 0.000s")
        self.sim_time_label.setStyleSheet("font-size: 11px; color: #007bff; font-weight: bold;")
        self.sim_time_label.setVisible(False)  # 默认隐藏，仿真时显示

        # 进度条（紧凑版）
        self.compact_progress = QProgressBar()
        self.compact_progress.setRange(0, 100)
        self.compact_progress.setValue(0)
        self.compact_progress.setMaximumHeight(16)
        self.compact_progress.setMaximumWidth(150)
        self.compact_progress.setStyleSheet("""
            QProgressBar {
                border: 1px solid #dee2e6;
                border-radius: 3px;
                text-align: center;
                font-size: 9px;
                color: #495057;
                background-color: #f8f9fa;
            }
            QProgressBar::chunk {
                background-color: #28a745;
                border-radius: 2px;
            }
        """)
        self.compact_progress.setVisible(False)  # 默认隐藏

        layout.addWidget(self.main_status)
        layout.addStretch()
        layout.addWidget(self.radar_mode_label)
        layout.addWidget(self.sim_time_label)
        layout.addWidget(self.compact_progress)

    def update_status(self, status: str, progress: int = None):
        """更新状态"""
        self.main_status.setText(status)

        if progress is not None and progress > 0:
            self.compact_progress.setVisible(True)
            self.compact_progress.setValue(progress)
        else:
            self.compact_progress.setVisible(False)

    def update_simulation_time(self, sim_time: float):
        """更新仿真时间显示"""
        self.sim_time_label.setText(f"仿真时间: {sim_time:.3f}s")
        self.sim_time_label.setVisible(True)

    def update_radar_mode(self, radar_mode: str):
        """更新雷达工作模式显示"""
        mode_display = {
            'search': '搜索',
            'track': '跟踪'
        }
        mode_text = mode_display.get(radar_mode, radar_mode)

        # 不同模式使用不同颜色
        mode_color = '#28a745' if radar_mode == 'search' else '#007bff'

        self.radar_mode_label.setText(f"雷达模式: {mode_text}")
        self.radar_mode_label.setStyleSheet(f"font-size: 11px; color: {mode_color}; font-weight: bold;")
        self.radar_mode_label.setVisible(True)

    def hide_simulation_time(self):
        """隐藏仿真时间显示"""
        self.sim_time_label.setVisible(False)
        self.radar_mode_label.setVisible(False)

    def set_ready(self):
        """设置就绪状态"""
        self.main_status.setText("系统就绪")
        self.main_status.setStyleSheet("font-size: 11px; color: #495057;")
        self.compact_progress.setVisible(False)

    def set_error_status(self, error_msg: str):
        """设置错误状态"""
        self.main_status.setText(f"错误: {error_msg}")
        self.main_status.setStyleSheet("font-size: 11px; color: #dc3545;")
        self.compact_progress.setVisible(False)

    def set_warning_status(self, warning_msg: str):
        """设置警告状态"""
        self.main_status.setText(f"警告: {warning_msg}")
        self.main_status.setStyleSheet("font-size: 11px; color: #ffc107;")
        self.compact_progress.setVisible(False)

    def set_success_status(self, success_msg: str):
        """设置成功状态"""
        self.main_status.setText(success_msg)
        self.main_status.setStyleSheet("font-size: 11px; color: #28a745;")
        self.compact_progress.setVisible(False)